import axios from 'axios'
import { ElMessage } from 'element-plus'

const request = axios.create({
  baseURL: import.meta.env.VITE_BASE_API,
  timeout: 5000,
  headers: {
    icode: '00CA9A70CBBA7C87'
  }
})

request.interceptors.response.use(
  (response) => {
    const { success, message, data } = response.data

    if (success) {
      return data
    } else {
      // 业务错误
      ElMessage.error(message)
      return Promise.reject(new Error(message))
    }

    return response
  },
  (error) => {
    ElMessage.error(error.message)
    return Promise.reject(error)
  }
)

export default request
